<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="8"
              type="flex"
              align="middle"
              style="margin-bottom:10px">

        <el-col :span="1.5">
          <el-button size="mini"
                     type="primary"
                     icon="el-icon-search"
                     @click="getData()">搜索</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button icon='el-icon-refresh'
                     size="mini"
                     @click="getData()">重置</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData"
                v-loading="loading"
                style="width: 100%"
                :header-cell-style="{background:'#f8f8f9','text-align':'center'}"
                :cell-style="{'text-align':'center'}">
        <el-table-column type="selection"
                         width="120">
        </el-table-column>
        <el-table-column prop="adviseId"
                         label="意见表id"
                         width="120">
        </el-table-column>
        <el-table-column prop="userId"
                         label="提交用户id"
                         width="120">
        </el-table-column>
        <el-table-column prop="nickName"
                         label="用户昵称">
        </el-table-column>
        <el-table-column prop="title"
                         label="标题">

        </el-table-column>
        <el-table-column prop="details"
                         label="内容"
                         width="130">
        </el-table-column>
        <el-table-column prop="picUrl"
                         label="url"
                         width="130">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px"
                      :src="scope.row.picUrl"
                      :preview-src-list="[scope.row.picUrl]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="ideaType"
                         label="建议/投诉">
          <template slot-scope="scope">
            <span>{{scope.row.ideaType==1?'建议':'支出'}}</span>
          </template>

        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="data">
            <el-button icon="el-icon-delete"
                       type="text"
                       @click="deleteHandler(data.row.adviseId)">删除</el-button>
          </template>

        </el-table-column>

      </el-table>
      <div class="page"
           v-show="total!=0">
        <el-pagination background
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryParams.pageNum"
                       :page-sizes="[3, 5, 7, 9]"
                       :page-size="queryParams.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryParams: { pageNum: 1, pageSize: 3 },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getData()
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getData()
    },
    async getData() {
      this.loading = true
      let { data } = await this.$axios.get('com/advise/list', { params: this.queryParams })
      console.log(data)
      this.tableData = data.rows
      this.loading = false
      this.total = data.total
    },
    deleteHandler(id) {
      this.$confirm(`是否确认删除意见编号为${id.toString()}的数据项?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.del(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async del(id) {
      let { data } = await this.$axios.delete(`com/advise/${id}`)
      if (data.code != 200) return this.$message.error(data.msg)
      this.$message({
        type: 'success',
        message: data.msg
      })
      this.getData()
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<style lang="scss" scoped>
.page {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>